/* 
Generate estimates for 20 mile increment rings around COUNTY centroids

Goal: produce coefficients for WORKERS, for 5 distance bands
Using WORKER-CENTRIC, WORKER data at COUNTY level

measuring impact of wind in surrounding counties on worker outcomes in a given county
*/

clear all
set more off
sysdir set PLUS /apps/shared/stata/e
cd /projects/users/########/AnalysisWorkerCentricCounty

capture log close
set logtype text
quietly log using "Logs/WrkrCentricCntyAggDisclosure", text replace

import sas "/projects/users/########/all_cntiesxmile_windwrkrs.sas7bdat", case(lower)
keep if !missing(_id_)
drop _merge

merge 1:1 ctyfips year using "/projects/users/########/cnty_dist_neariv.dta"
keep if _merge==3
drop _merge

drop if year==2015

* Some basic data cleaning for variables we may use

egen county = group(ctyfips)
xtset county year

gen ccap = c_tcap/1000


bysort ctyfips: egen mxcap = max(ccap)
*bysort cnty: egen mxcap = max(ccap)
sort ctyfips year

gen bigplnt = 0
replace bigplnt = 1 if mxcap >= 10
gen treat = 0
replace treat = 1 if mxcap >=10 & ccap >=10

* make capacity in GW
replace ccap = ccap/1000
label var ccap "Capacity (GW)"

* For county worker-centric (propagating inward)
rename total_earn total_wages

gen ln_totalwages_c = log(1+total_wages)

gen ln_totalemp_c = log(1+total_emp)

gen ln_dlpcap_c = ln_totalwages_c - ln_totalemp_c

*gen ln_tot_whiteemp_c = log(1+total_white_emp)
*gen ln_tot_nonwhiteemp_c = log(1+total_nonwhite_emp)

gen ccap20 = c_tcap20/1000000
label var ccap20 "20 Mile Capacity (GW)"
gen ccap40 = c_tcap40/1000000
label var ccap40 "40 Mile Capacity (GW)"
gen ccap60 = c_tcap60/1000000
label var ccap60 "60 Mile Capacity (GW)"
gen ccap80 = c_tcap80/1000000
label var ccap80 "80 Mile Capacity (GW)"
gen ccap100 = c_tcap100/1000000
label var ccap100 "100 Mile Capacity (GW)"

/*
gen treat20 = 0
replace treat20 = 1 if ccap20 >=0.010
gen treat40 = 0
replace treat40 = 1 if ccap40 >=0.010
gen treat60 = 0
replace treat60 = 1 if ccap60 >=0.010
gen treat80 = 0
replace treat80 = 1 if ccap80 >=0.010
gen treat100 = 0
replace treat100 = 1 if ccap100 >=0.010
*/


gen d_cap40 = ccap40 - ccap20
label var d_cap40 "20 to 40 Mile Capacity"
gen d_cap60 = ccap60 - ccap40
label var d_cap60 "40 to 60 Mile Capacity"
gen d_cap80 = ccap80 - ccap60
label var d_cap80 "60 to 80 Mile Capacity"
gen d_cap100 = ccap100 - ccap80
label var d_cap100 "80 to 100 Mile Capacity"

gen dz40 = zcnty40 - zcnty20
gen dz60 = zcnty60 - zcnty40
gen dz80 = zcnty80 - zcnty60
gen dz100 = zcnty100 - zcnty80

tabulate year, gen(ydum)

**********************************************************
* county worker-centric (propagating inward) regressions:
* (no lags. adding time lags of dependent variable is also interesting) 

foreach var of varlist ln_dlpcap_c ln_totalwages_c ln_totalemp_c {
	foreach num of numlist 20 40 60 80 100 {
		xtivreg2 `var' ydum* L.mean_age L.mean_female L.mean_nohigh L.mean_highsch ///
		L.mean_somecoll L.mean_college L.mean_hisp L.mean_black L.mean_white ///
		(ccap`num' = zcnty`num') , fe cluster(cnty)
		estadd scalar Nround = round(e(N),100)
		estadd scalar KP_F = round(e(widstat),1)
		estadd scalar nclust = round(e(N_clust),10)
		estadd scalar gavg = round(e(g_avg),1)
		est sto `var'`num'ctrl

		xtivreg2 `var' ydum* (ccap`num' = zcnty`num') if e(sample) , fe cluster(cnty)
		estadd scalar Nround = round(e(N),100)
		estadd scalar KP_F = round(e(widstat),1)
		estadd scalar nclust = round(e(N_clust),10)
		estadd scalar gavg = round(e(g_avg),1)
		est sto `var'`num'
	}
	esttab `var'20 `var'40 `var'60 `var'80 `var'100 ///
	using "Tables/WorkersCapInCounty`var'.csv", ///
	replace keep(ccap*) cells( b(star fmt(a2))  se(par  fmt(a2)) ) label  ///
	star(* 0.10 ** 0.05 *** 0.01) nonumbers mtitles(m20 m40 m60 m80 m100)   ///
	stat(Nround r2 KP_F N_g nclust g_min gavg g_max) ///
	title (Inward Impact of Wind on `var' by distance no controls XTIV: County-Level Data) ///
	addnotes(Std. errors clustered at the worker level. * 0.10 ** 0.05 *** 0.01.) 

	esttab `var'20ctrl `var'40ctrl `var'60ctrl `var'80ctrl `var'100ctrl ///
	using "Tables/WorkersCapInCountyCtrl`var'.csv", ///
	replace keep(ccap*) cells( b(star fmt(a2))  se(par  fmt(a2)) ) label  ///
	star(* 0.10 ** 0.05 *** 0.01) nonumbers mtitles(m20 m40 m60 m80 m100)   ///
	stat(Nround r2 KP_F N_g nclust g_min gavg g_max) ///
	title (Inward Impact of Wind on `var' by distance county controls XTIV: County-Level Data) ///
	addnotes(Std. errors clustered at the worker level. * 0.10 ** 0.05 *** 0.01.) 
}

quietly log close


quietly log using "Logs/CntyInWrkrAggSummStats", text replace

gen sal_c = exp(ln_dlpcap_c)

replace ccap20 = ccap20*1000
replace ccap40 = ccap40*1000
replace ccap60 = ccap60*1000
replace ccap80 = ccap80*1000
replace ccap100 = ccap100*1000
replace d_cap40 = d_cap40*1000
replace d_cap60 = d_cap60*1000
replace d_cap80 = d_cap80*1000
replace d_cap100 = d_cap100*1000

tabstat total_wages total_emp sal_c ccap20 ccap40 ccap60 ccap80 ccap100 ///
	mean_age mean_female mean_nohigh mean_highsch ///
	mean_somecoll mean_college mean_hisp mean_black mean_white ///
	d_cap40 d_cap60 d_cap80 d_cap100 ///
	, s(mean sd) c(s) f(%12.2f)
	
	
quietly log close

